package com.baturu.mybatisminus.util;

import com.baturu.mybatisminus.annotation.Column;
import com.baturu.mybatisminus.annotation.Id;
import com.baturu.mybatisminus.annotation.Table;
import com.baturu.mybatisminus.annotation.Transient;
import com.baturu.mybatisminus.annotation.info.ColumnAnnotation;
import com.baturu.mybatisminus.annotation.info.IdAnnotation;
import com.baturu.mybatisminus.annotation.info.TableAnnotation;
import com.baturu.mybatisminus.annotation.info.TransientAnnotation;

import java.lang.reflect.Field;

/**
 *
 * @author huangbin
 */
public class AnnotationUtils {

    private static boolean jpaAnnotationPresent;

    static {
        try {
            Class.forName("javax.persistence.Column");
            jpaAnnotationPresent = true;
        } catch (Exception ignored) {}
    }


    public static IdAnnotation parseIdAnnotation(Field field) {
        Id id = field.getAnnotation(Id.class);
        if(id != null) {
            return new IdAnnotation();
        }
        if(jpaAnnotationPresent) {
            javax.persistence.Id jpaId = field.getAnnotation(javax.persistence.Id.class);
            if(jpaId != null) {
                return new IdAnnotation();
            }
        }
        return null;
    }

    public static ColumnAnnotation parseColumnAnnotation(Field field) {
        Column column = field.getAnnotation(Column.class);
        if (column != null) {
            return ColumnAnnotation.builder()
                    .emptyStringAsNull(column.emptyStringAsNull())
                    .insertable(column.insertable())
                    .updatable(column.updatable())
                    .typeHandler(column.typeHandler())
                    .values(column.value())
                    .build();
        }
        if (jpaAnnotationPresent) {
            javax.persistence.Column jpaColumn = field.getAnnotation(javax.persistence.Column.class);
            if (jpaColumn != null) {
                return ColumnAnnotation.builder()
                        .insertable(jpaColumn.insertable())
                        .updatable(jpaColumn.updatable())
                        .values(jpaColumn.name())
                        .build();
            }
        }
        return null;
    }


    public static TransientAnnotation parseTransientAnnotation(Field field) {
        Transient silent = field.getAnnotation(Transient.class);
        if(silent != null) {
            return new TransientAnnotation();
        }
        if(jpaAnnotationPresent) {
            javax.persistence.Transient jpaSilent = field.getAnnotation(javax.persistence.Transient.class);
            if(jpaSilent != null) {
                return new TransientAnnotation();
            }
        }
        return null;
    }


    public static TableAnnotation parseTableAnnotation(Class<?> clazz) {
        Table table = clazz.getAnnotation(Table.class);
        if(table != null) {
            return TableAnnotation.builder().values(table.value()).build();
        }
        if(jpaAnnotationPresent) {
            javax.persistence.Table jpaTable = clazz.getAnnotation(javax.persistence.Table.class);
            if(jpaTable != null) {
                return TableAnnotation.builder().values(jpaTable.name()).build();
            }
        }
        return null;
    }




}
